package com.mint.foobar.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by larry on 2016/10/09.
 * description
 */
@RestController
public class FoobarController {
    private static final Logger log = LoggerFactory.getLogger(FoobarController.class);

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/serviceurl")
    public Map<String, List<ServiceInstance>> serviceUrl() {
        Map<String, List<ServiceInstance>> msl = new HashMap<>();
        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            List<ServiceInstance> sis = discoveryClient.getInstances(service);
            msl.put(service, sis);
        }
        return msl;
    }

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String index(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        String s = String.format("foobar->method:%1$s id:%2$s port:%3$s time:%4$s",
                httpServletRequest.getMethod(),
                this.discoveryClient.getLocalServiceInstance().getServiceId(),
                this.discoveryClient.getLocalServiceInstance().getPort(),
                System.currentTimeMillis());
        return s;
    }

    @HystrixCommand(fallbackMethod = "fallback")
    @RequestMapping(value = "/message", method = RequestMethod.GET)
    public String getMessage() throws Exception {
        String bar = this.getMessageFromBarService();
        String foo = this.getMessageFromFooService();
        String foobar = String.format("%1$s\r\n%2$s", foo, bar);
        log.debug("Result foobar message: {}.", foobar);
        return foobar;
    }

    private String getMessageFromBarService() throws Exception {
        //TODO 此处不能加入ip（域名）端口
        String bar = restTemplate.getForObject("http://bar/message", String.class);
        log.debug("From bar service : {}.", bar);
        return bar;
    }

    private String getMessageFromFooService() throws Exception {
        String foo = restTemplate.getForObject("http://foo/message", String.class);
        log.debug("From foo service : {}.", foo);
        return foo;
    }

    //断路器回调
    private String fallback() {
        log.debug("fallback");
        return "fallback";
    }

}
